package org.mullin.stratego.shared;

public class Move {
	public Coord to, from;
	
	public Move(Coord to, Coord from) {
		this.to = to;
		this.from = from;
	}
	
	/**
	 * Serialize a move into a string of the form to.x, to.y, from.x, from.y
	 * 
	 * @param m Move
	 * @return serialized move
	 */
	public static String serialize(Move m) {
		StringBuilder sb = new StringBuilder();
		sb.append(Integer.toString(m.to.x))
			.append(Integer.toString(m.to.y))
			.append(Integer.toString(m.from.x))
			.append(Integer.toString(m.from.y));
		return sb.toString();
	}
	
	public static Move deserialize(String s) {
		int x, y;		
		
		x = Character.digit(s.charAt(0), 10);
		y = Character.digit(s.charAt(1), 10);
		Coord t = new Coord(x, y);
		
		x = Character.digit(s.charAt(2), 10);
		y = Character.digit(s.charAt(3), 10);
		Coord f = new Coord(x, y);
		return new Move(t, f);
	}
	
	public String toString() {
		return "to=" + to + ", from=" + from;
	}
}
